<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display();
    }

    public function articlelist(){
        //查询文章
        $news = M("articles")->select();
        $type = M("articletype")->select();

        $this->assign("type",$type);
        $this->assign("news",$news);
        $this->display();
    }

    //查询修改内容
    public function articleedit(){
        $articleId = $_GET['articleId'];
        $news = M("articles")->where("articleId={$articleId}")->find();
        print_r($news);

        $types = M("articletype")->select();
        $this->assign("types",$types);
        $this->assign("news",$news);
        $this->display();
    }
    //修改文章内容
    public function updatenews($articleId){
        $save1 = $_POST['save1'];
        if ($save1 == 'no'){
            $date = array(
                "title" => $_POST['title'],
                "content" =>$_POST['content'],
                "shortcontent" =>$_POST['shortcontent'],
                "keywords" =>$_POST["keywords"],
                "typeid" =>$_POST["type"]
            );
            $result = M("articles")->where("articleId={$articleId}")->save($date);

            if ($result > 0){
                $this->success("修改记录成功!","Index/articlelist.html");
            }else{
                $this->success("修改记录失败!","Index/articlelist.html");
            }
        }elseif ($save1 == 'yes'){
            $upload = new \Think\Upload();
            $upload->maxSize = 1000000;
            $upload->exts =array('jpg','gif','png','jpeg');
            $upload->rootPath = './Uplods/';
            $upload->savePath = '';

            $upinfo = $upload->upload();
            if (!$upinfo){
                $this->error($upload->getError());
            }else{
                $this->success('上传成功！');
                $date = array(
                    "title" => $_POST['title'];
                    "content" => $_POST['content'];
                    "shortcontent" => $_POST['shortcontent'];
                    "imagepath" => $upinfo[0]['savename'];
                    "keywords" => $_POST['keywords'];
                    "typeid" => $_POST['type']
                );

                $test = M("articles");
                $result = $test->where("articleId={$articleId}")->save($date);

                if ($result > 0){
                    $this->success("修改记录成功","Index/articlelist.html");
                }else{
                    $this->success("修改记录失败","Index/articlelist.html");
                }
            }
        }
    }

    //删除记录
    public function articledel(){
        $articleId = $_GET["articleId"];

        $result = M("articles")->where("articleId={$articleId}")->delete();

        if ($result > 0){
            $this->success("删除记录成功！","Index/articlelist.html");
        }else{
            $this->success("删除记录失败!","Index/articlelist.html");
        }
    }


    public function articleadd(){
        $type = M("articletype")->select();

        $this->assign("type",$type);
        $this->display();

    }


    public function add(){
        $uplod= new \Think\Upload();
        $uplod->maxSize = 100000;
        $uplod->exts = array('jpg','gif','png','jpeg');
        $uplod->rootPath = './Uploads/';
        $uplod->savePath = "public/newsImage/";
        $uplod->saveName = array('uniqid','');
        $uplod->autoSub =true;

        $info_uplod = $uplod->upload();

        if (!$info_uplod){
            $this->error($uplod->getError());
        }else{
            foreach ($info_uplod as $info){
                $info_path = './Uplods'.$info['savePath'].$info['saveName'];
            }
            $image = new \Think\Upload();
            $image->open($info_path);

            $width = $image->width();
            $height = $image->height();
            $type = $image->type();
            $mime = $image->mime();
            $size = $image->size();
            $image->crop(400,400)->save('./crop.jpg');

            $date = array(
                "title" => $_POST['title'],

            );
        }
    }
}